/*
 * Copyright (c) 2024 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
 * easyAda is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */


#include <arch/cpu.h>
#include <hal/interrupt_ctrl.h>
#include <arch/scr.h>
#include <core/dprintf.h>

void set_vectors()
{
    extern uint64_t vectors_entry;
    uint64_t        vector_base     = (uint64_t)&vectors_entry;
    uint8_t         exception_level = (scr_read_CurrentEL() >> 2) & 0X3;
    dprintf("vector_base:%x\n", vector_base);
    assert(0 == (vector_base & lowbitsmask(11)));
    switch (exception_level) {
        case 0:
            dprintf("no vectors in level0!\n");
            break;
        case 1:
            scr_write_vbar_el1(vector_base);
            break;
        case 2:
            scr_write_vbar_el2(vector_base);
            break;
        case 3:
            scr_write_vbar_el3(vector_base);
            break;
        default:
            dprintf("set_vector error exception level!\n");
            break;
    }
}

void cpu_init(void)
{
    scr_enable_cpu_cache();
    interrupt_init_local_controller();
    scr_dump_info();

    set_vectors();
}
